package com.auicyh.tylb.dialog;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.auicyh.tylb.R;
import com.auicyh.tylb.utils.StringUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.util.HashMap;
import java.util.Map;

/**
 * 作者：  Ocean 2018/8/28 9:15
 * 信息：  Oil com.auicyh.tylb.dialog
 * 功能：  二维码对话框
 * 邮箱：   1126067113@qq.com
 * 修改人：
 * 公司: 北京天元陆兵汽车科技有限公司
 * @author Ocean
 */
public class DialogQrCodeFragment extends BaseDialogFragment {
    /** 要生产的二维码内容 */
    public String mQrCodeStr;
    /** 二维码内容Key */
    public static final String KEY_QR_CODE_STR = "key_qr_code_str";

    /**
     * @param qrCodeStr 二维码内容
     * @return DialogQrCodeFragment
     */
    public static DialogQrCodeFragment newInstance(String qrCodeStr) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_QR_CODE_STR, qrCodeStr);
        DialogQrCodeFragment qrCodeFragment = new DialogQrCodeFragment();
        qrCodeFragment.setArguments(bundle);
        return qrCodeFragment;
    }

    public DialogQrCodeFragment() {
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mQrCodeStr = bundle.getString(KEY_QR_CODE_STR);
        }
    }

    @Override
    public void onViewDialog(FrameLayout frameLayout) {
        View view = View.inflate(getActivity(), R.layout.dialog_qrcode_fragment, null);
        frameLayout.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        TextView textView = view.findViewById(R.id.tylb_dialog_hint_tv);
        if (!StringUtils.isStrNull(getMessage())) {
            textView.setText(getMessage());
        }
        if (StringUtils.isStrNull(mQrCodeStr)) {
            mQrCodeStr = "www.auicyh.com";
        }
        Bitmap bitmap = generateBitmap(mQrCodeStr, 700, 700);
        ImageView qrCodeIv = view.findViewById(R.id.tylb_dialog_qr_code_iv);
        Bitmap logo = BitmapFactory.decodeResource(getResources(), R.drawable.tylb_wx);
        Bitmap bitmapLogo = null;
        if (bitmap != null) {
            bitmapLogo = addLogo(bitmap, logo);
        }
        qrCodeIv.setImageBitmap(bitmapLogo);

    }

    private Bitmap generateBitmap(String content, int width, int height) {
        if (StringUtils.isStrNull(content)) {
            content = "AUI";
        }
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, String> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        try {
            BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    if (encode.get(j, i)) {
                        pixels[i * width + j] = 0x00000000;
                    } else {
                        pixels[i * width + j] = 0xffffffff;
                    }
                }
            }
            return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }

    private Bitmap addLogo(Bitmap qrBitmap, Bitmap logoBitmap) {
        int qrBitmapWidth = qrBitmap.getWidth();
        int qrBitmapHeight = qrBitmap.getHeight();
        int logoBitmapWidth = logoBitmap.getWidth();
        int logoBitmapHeight = logoBitmap.getHeight();
        Bitmap blankBitmap = Bitmap.createBitmap(qrBitmapWidth, qrBitmapHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(blankBitmap);
        canvas.drawBitmap(qrBitmap, 0, 0, null);
        canvas.save();
        float scaleSize = 1.0f;
        while ((logoBitmapWidth / scaleSize) > (qrBitmapWidth / 5) || (logoBitmapHeight / scaleSize) > (qrBitmapHeight / 5)) {
            scaleSize *= 2;
        }
        float sx = 2.0f / scaleSize;
        canvas.scale(sx, sx, qrBitmapWidth / 2, qrBitmapHeight / 2);
        canvas.drawBitmap(logoBitmap, (qrBitmapWidth - logoBitmapWidth) / 2, (qrBitmapHeight - logoBitmapHeight) / 2, null);
        canvas.restore();
        return blankBitmap;
    }

    public String getQrCodeStr() {
        return mQrCodeStr;
    }

    public void setQrCodeStr(String qrCodeStr) {
        mQrCodeStr = qrCodeStr;
    }
}
